/*
 * Copyright 2025 The JA-SIG Collaborative. All rights reserved.
 * distributed with this file and available online at
 */
package com.lap.auth.adapter.driven.web.region;

import com.lap.auth.application.region.RegionAppService;
import com.lap.auth.application.region.command.BatchCreateRegionCmd;
import com.lap.auth.application.region.command.BatchUpdateRegionCmd;
import com.lap.auth.application.region.command.UpdateRegionCmd;
import com.lap.auth.application.region.dto.GetRegionByIdsQuery;
import com.lap.auth.application.region.dto.RegionDto;
import com.lap.auth.application.region.dto.RegionOptionDto;
import com.lap.auth.application.region.dto.RegionViewDto;
import com.lap.framework.common.annotation.AuthSkip;
import com.lap.watchdog.starter.annotation.RepeatSubmit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 提供区域restful接口层:
 *
 * <ol>
 *   <li>业务规则，原则上不做任何业务逻辑处理，仅仅是技术入口
 *   <li>命名规则，语义上明确接口做什么，推荐使用 Restful 命名 + Verb风格方法名（适配Swagger、Spring）:
 *       <ul>
 *         <li>分页使用 getXXXPage
 *         <li>获取单个 getXXById
 *         <li>保存使用 createXX
 *         <li>更新使用 updateXX
 *         <li>删除使用 deleteXX
 *       </ul>
 * </ol>
 *
 * @author Shuisheng Lao(劳水生)
 * @version 0.0.1
 */
@Tag(name = "区域管理", description = "提供区域基本操作，查询等")
@RequiredArgsConstructor
@RestController
@RequestMapping("api/v1/regions")
public class RegionController {

  private final RegionAppService regionAppService;

  @Operation(summary = "获取区域树")
  @AuthSkip
  @GetMapping
  public List<RegionDto> getRegionTree() {
    return regionAppService.getRegionTree();
  }

  @Operation(summary = "获取区域")
  @GetMapping("{parentId}")
  public RegionViewDto getRegions(@PathVariable Integer parentId) {
    return regionAppService.queryRegions(parentId);
  }

  @Operation(summary = "根据ids获取区域")
  @AuthSkip
  @PostMapping("_batch/query")
  public List<RegionOptionDto> batchGetRegions(@RequestBody @Valid GetRegionByIdsQuery query) {
    return regionAppService.queryRegionsByIds(query);
  }

  @Operation(summary = "批量添加子区域")
  @RepeatSubmit
  @PostMapping("_batch")
  public void batchCreateRegion(@RequestBody @Valid BatchCreateRegionCmd cmd) {
    regionAppService.batchCreateRegions(cmd);
  }

  @Operation(summary = "批量更新子区域")
  @RepeatSubmit
  @PutMapping("_batch/{id}")
  public void batchUpdateRegion(
      @PathVariable Integer id, @RequestBody @Valid BatchUpdateRegionCmd cmd) {
    regionAppService.batchUpdateRegions(id, cmd);
  }

  @Operation(summary = "更新区域")
  @RepeatSubmit
  @PutMapping("{id}")
  public Integer updateRegion(@PathVariable Integer id, @RequestBody @Valid UpdateRegionCmd cmd) {
    return regionAppService.updateRegion(id, cmd);
  }

  @Operation(summary = "删除区域")
  @RepeatSubmit
  @DeleteMapping("{id}")
  public Integer deleteRegion(@PathVariable Integer id) {
    return regionAppService.deleteRegion(id);
  }
}
